package com.apt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.apt.common.AptException;
import com.apt.common.DBUtility;
import com.apt.domain.AnswerMaster;
import com.apt.domain.QuestionMaster;
import com.apt.domain.TestMaster;
import com.apt.domain.UserMaster;

public class LoadTestDao extends BaseConnection {

	public LoadTestDao(Connection conn) {
		super(conn);
	}

	public int insertTest(TestMaster testMaster) {
		int success = -1;

		try {
			/*System.out
					.println("inside dao - " + testMaster.getTotalQuestions());*/
			PreparedStatement ps = connection
					.prepareStatement("INSERT INTO TEST_MASTER (NAME,TEST_TYPE_ID,TOTAL_QUESTIONS,DISABLED,DURATION,DESCRIPTION)VALUES"
							+ "(?,?,?,?,?,?)");
			ps.setString(1, testMaster.getName());
			ps.setInt(2, testMaster.getTestTypeId());
			ps.setInt(3, testMaster.getTotalQuestions());
			ps.setString(4, testMaster.getDisabled());
			ps.setInt(5, testMaster.getDuration());
			ps.setString(6, testMaster.getName());
			success = ps.executeUpdate();
			ps.close();
			connection.commit();

		} catch (SQLException se) {
			se.printStackTrace();
		} finally {
			DBUtility.cleanUp(connection);
		}
		return success;
	}

	public int getTestId(String testName) throws AptException {
		int testId;

		try {
			PreparedStatement ps = connection
					.prepareStatement("SELECT TEST_ID FROM TEST_MASTER WHERE NAME = ? ");
			ps.setString(1, testName);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				testId = rs.getInt("TEST_ID");
			} else {
				throw new AptException(
						"No test found for the given test name - " + testName);
			}
			rs.close();
		} catch (SQLException se) {
			se.printStackTrace();
			throw new AptException(
					"unable to get test id from test master table");
		} finally {
			DBUtility.cleanUp(connection);
		}
		return testId;
	}

	public int insertQuestion(QuestionMaster qm) {
		int success = -1;

		try {

			PreparedStatement ps = connection
					.prepareStatement("INSERT INTO QUESTION_MASTER (QUESTION,TEST_ID,SECTION) VALUES (?,?,?)");
			ps.setString(1, qm.getQuestion());
			ps.setInt(2, qm.getTestId());
			ps.setString(3, qm.getSection());
			success = ps.executeUpdate();
			ps.close();
			connection.commit();

		} catch (SQLException se) {
			se.printStackTrace();
		} finally {
			DBUtility.cleanUp(connection);
		}
		return success;
	}

	public int getQuestionId(String question, int testId) throws AptException {
		int questionId;

		try {
			PreparedStatement ps = connection
					.prepareStatement("SELECT QUESTION_ID FROM QUESTION_MASTER WHERE QUESTION = ? AND TEST_ID = ?");
			ps.setString(1, question);
			ps.setInt(2, testId);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				questionId = rs.getInt("QUESTION_ID");
			} else {
				throw new AptException(
						"No question id found for the given question - "
								+ question);
			}
			rs.close();
		} catch (SQLException se) {
			se.printStackTrace();
			throw new AptException(
					"unable to get question id from question master table");
		} finally {
			DBUtility.cleanUp(connection);
		}
		return questionId;
	}

	public void insertAnswer(List<AnswerMaster> answerList) {

		try {

			PreparedStatement ps = connection
					.prepareStatement("INSERT INTO ANSWER_MASTER (QUESTION_ID,ANSWER,ANSWER_CORRECT) VALUES (?,?,?)");
			for (AnswerMaster am : answerList) {
				ps.setInt(1, am.getQuestionId());
				ps.setString(2, am.getAnswer());
				ps.setString(3, am.getAnswerCorrect());
				ps.addBatch();
			}
			ps.executeBatch();
			ps.close();
			connection.commit();

		} catch (SQLException se) {
			se.printStackTrace();
		} finally {
			DBUtility.cleanUp(connection);
		}

	}

}
